package com.spt.springbootzhsx.service;

import com.spt.springbootzhsx.dao.OrderDao;
import com.spt.springbootzhsx.entity.Order;
import com.spt.springbootzhsx.entity.back.OrderBack;
import com.spt.springbootzhsx.mapper.OrderMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;

@Service
public class OrderImpl implements OrderService{

    private final OrderMapper orderMapper;
    @Autowired
    private OrderDao orderDao;

    public OrderImpl(OrderMapper orderMapper) {
        this.orderMapper = orderMapper;
    }

    @Override
    public ArrayList<OrderBack> getAllOrder() {
        return orderDao.getAllOrder();
    }

    @Override
    public String addOrder(Order order) {
        SimpleDateFormat simpleDateFormat=new SimpleDateFormat("yy-MM-dd HH:mm:ss");
        String time=simpleDateFormat.format(new Date());
        try {
            order.setBuyTime(simpleDateFormat.parse(time));
        } catch (ParseException e) {
            e.printStackTrace();
        }
        int k=orderMapper.addOrder(order);
        if(k>=1){
            return "订单添加成功！";
        }else{
            return "订单添加失败！";
        }
    }


    @Override
    public ArrayList<Order> getOrderByPid(Integer pid) {
        return orderMapper.getAllOrderByPid(pid);
    }

    @Override
    public ArrayList<OrderBack> getOrderHistory(Integer uid) {
        return orderDao.getOrderHistory(uid);
    }

    @Override
    public String deleteOrder(Integer oid) {
        int k=orderMapper.deleteOrder(oid);
        if(k>=1){
            return "订单删除成功！";
        }else{
            return "订单删除失败！";
        }
    }

    @Override
    public ArrayList<OrderBack> getOrderByUnameOrMname(String key) {
        ArrayList<OrderBack> ulist=orderDao.getOrderByUsername(key);
        if(ulist.size()!=0) {
            //根据用户名搜索
            return ulist;
        }
        //否则根据电影名搜索
        ArrayList<OrderBack> mlist=orderDao.getOrderByMname(key);
        return mlist;
    }
}
